﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace YArchitech.HVAC
{
	public class CEncrypt
	{
		public static string Encrypt(string pToEncrypt)
		{
			string s = "revitmep";
			DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
			byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
			descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(s);
			descryptoServiceProvider.IV = Encoding.ASCII.GetBytes(s);
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
			cryptoStream.Write(bytes, 0, bytes.Length);
			cryptoStream.FlushFinalBlock();
			StringBuilder stringBuilder = new StringBuilder();
			foreach (byte b in memoryStream.ToArray())
			{
				stringBuilder.AppendFormat("{0:X2}", b);
			}
			stringBuilder.ToString();
			return stringBuilder.ToString();
		}

		public static string DesEncrypt(string inputString)
		{
			return CEncrypt.DesEncrypt(inputString, CEncrypt.Key);
		}

		public static string DesDecrypt(string inputString)
		{
			return CEncrypt.DesDecrypt(inputString, CEncrypt.Key);
		}

		private static string Key
		{
			get
			{
				return "revitmep";
			}
		}

		public static string DesEncrypt(string inputString, string encryptKey)
		{
			byte[] rgbIV = new byte[]
			{
				18,
				52,
				86,
				120,
				144,
				171,
				205,
				239
			};
			string result;
			try
			{
				byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
				DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
				byte[] bytes2 = Encoding.UTF8.GetBytes(inputString);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
				cryptoStream.Write(bytes2, 0, bytes2.Length);
				cryptoStream.FlushFinalBlock();
				result = Convert.ToBase64String(memoryStream.ToArray());
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public static string DesDecrypt(string inputString, string decryptKey)
		{
			byte[] rgbIV = new byte[]
			{
				18,
				52,
				86,
				120,
				144,
				171,
				205,
				239
			};
			byte[] array = new byte[inputString.Length];
			string result;
			try
			{
				byte[] bytes = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
				DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
				array = Convert.FromBase64String(inputString);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write);
				cryptoStream.Write(array, 0, array.Length);
				cryptoStream.FlushFinalBlock();
				result = new UTF8Encoding().GetString(memoryStream.ToArray());
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}
	}
}
